using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BlogEngine.Core.Providers;
using BlogEngine.Core;

public partial class gallery_editalbum : System.Web.UI.Page
{
	protected Guid guid;

	protected void Page_Load(object sender, EventArgs e)
	{
		guid = new Guid(Request.QueryString["AlbumId"]);

		if (!Page.IsPostBack && !Page.IsCallback)
			BindPage();

		Page.Title = Resources.labels.photos;

		btnAlbum.Click += new EventHandler(btnAlbum_Click);

		valExist.ServerValidate += new ServerValidateEventHandler(valExist_ServerValidate);
	}

	#region Event handlers

	private void valExist_ServerValidate(object source, ServerValidateEventArgs args)
	{
		args.IsValid = true;

		foreach (Album al in Album.Items)
			if (al.Title.Equals(txtTitle.Text.Trim(), StringComparison.OrdinalIgnoreCase))
				args.IsValid = false;
	}

	/// <summary>
	/// Saves the album
	/// </summary>
	private void btnAlbum_Click(object sender, EventArgs e)
	{
		if (Page.IsValid)
			if (txtTitle.Text.Trim().Length != 0)
			{
				Album album = Album.GetById(guid);
				album.Title = txtTitle.Text.Trim();
				album.Description = txtDescription.Text.Trim();
				album.IsPublished = cbIsPublished.Checked;
				album.Slug = txtSlug.Text.Trim();
				album.TagString = txtTags.Text;
				album.Save();
			}
	}

	#endregion

	#region Data Binding

	private void BindPage()
	{
		Album album = Album.GetById(guid);		
		txtTitle.Text = album.Title;
		txtDescription.Text = album.Description;
		txtSlug.Text = album.Slug;
		cbIsPublished.Checked = album.IsPublished;
		txtTags.Text = album.TagString;
	}

	#endregion


}
